package com.byjh.app.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.ibatis.cursor.Cursor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.byjh.app.mapper.QuestionMapper;
import com.byjh.app.domain.Question;
import com.byjh.app.service.IQuestionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 题目Service业务层处理
 *
 * @author Dhq
 * @date 2025-01-15
 */
@Service
public class QuestionServiceImpl implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询题目
     *
     * @param id 题目主键
     * @return 题目
     */
    @Override
    public Question selectQuestionById(Long id) {
        return questionMapper.selectQuestionById(id);
    }


    /**
     * 查询易错误99题目列表
     *
     * @return 题目列表
     */
    @Override
    public List<Question> selectError99QuestionList(){
        return questionMapper.selectError99QuestionList();
    }

    /**
     * 用户错题集
     * @param userId
     * @return
     */
    @Override
    public
    List<Question> selectErrorCollectSetQuestionList(Long userId,Long chapterId,Integer type){
        return questionMapper.selectErrorCollectSetQuestionList(userId,chapterId,type);
    }


    /**
     * 生成考试试卷
     *
     * @param examSize 试卷题目数量
     * @return 试卷题目列表
     */
    @Override
    @Transactional(readOnly = true)
    public List<Question> generateExamPaper(int examSize) {
        List<Question> examPaper = new ArrayList<>();
        Random random = new Random();
        int count = 0;

        Cursor<Question> cursor = questionMapper.selectQuestionListCursor();
        if (cursor == null) {
            throw new RuntimeException("题库为空");
        }
        // -1 dao中没有设置最大题目数量 则不限制题目数量
        if (examSize == -1) {
            for (Question question : cursor) {
                examPaper.add(question);
            }
        } else {
            for (Question question : cursor) {
                count++;
                // 蓄水池抽样算法
                if (count <= examSize) {
                    examPaper.add(question);
                } else {
                    int j = random.nextInt(count);
                    if (j < examSize) {
                        examPaper.set(j, question);
                    }
                }
            }
        }


        return examPaper;
    }

    /**
     * 查询题目列表
     *
     * @param question 题目
     * @return 题目
     */
    @Override
    public List<Question> selectQuestionList(Question question) {
        return questionMapper.selectQuestionList(question);
    }

    /**
     * 新增题目
     *
     * @param question 题目
     * @return 结果
     */
    @Override
    public int insertQuestion(Question question) {
        return questionMapper.insertQuestion(question);
    }

    /**
     * 修改题目
     *
     * @param question 题目
     * @return 结果
     */
    @Override
    public int updateQuestion(Question question) {
        return questionMapper.updateQuestion(question);
    }

    /**
     * 更新错题次数
     *
     * @param id 题目主键
     * @return 结果
     */
    @Override
    public int updateErrorCount(Long id) {
        return questionMapper.updateErrorCount(id);
    }

    /**
     * 批量删除题目
     *
     * @param ids 需要删除的题目主键
     * @return 结果
     */
    @Override
    public int deleteQuestionByIds(Long[] ids) {
        return questionMapper.deleteQuestionByIds(ids);
    }

    /**
     * 删除题目信息
     *
     * @param id 题目主键
     * @return 结果
     */
    @Override
    public int deleteQuestionById(Long id) {
        return questionMapper.deleteQuestionById(id);
    }
}
